package com.rongke.web.controller.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.FailException;
import com.rongke.commons.JsonResp;
import com.rongke.model.User;
import com.rongke.service.UserService;
import com.rongke.utils.DateUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * @UserController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/user")
@Transactional
public class UserController {
    private Logger      log = Logger.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @Autowired
    private SessionDAO  sessionDAO;

    /**
     * @修改
     * @param user
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateUser(@RequestBody User user) {
        log.debug("updateUser");
        try {
            User userDo = new User();
            userDo.setUserId(user.getUserId());
            userDo.setSex(user.getSex());
            userDo.setAge(user.getAge());
//            userDo.set

            userService.updateById(user);
            return JsonResp.ok(user);
        } catch (Exception e) {
            log.error("updateUser failed", e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @根据id查找
     * @param phone
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectUser(String phone) {
        log.debug("查找");
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.eq("phone", phone);
        User user = userService.selectOne(ew);
        return JsonResp.ok(user);
    }

}
